package com.future.myjavatools.core.zhoupu;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author zujul
 * @date 2017/8/23
 * @remark
 */
public class StringUtil {

    public final static String[] EMPTY_STRING_ARRAY = {};
    public final static long[] EMPTY_LONG_ARRAY = {};
    public final static int[] EMPTY_INT_ARRAY = {};
    public final static String ENC_UTF8 = "UTF-8";
    public final static String EMPTY = "";
    public final static String COMMA = ",";
    public final static String SEMICOLON = ";";
    public final static String VERTICAL_BAR = "|";
    public final static String UNDERLINE = "_";
    public final static String COLON = ":";
    public final static String POUND_SIGN = "#";
    public final static Joiner DEFAULT_JOINER = Joiner.on(VERTICAL_BAR).useForNull("-");
    public final static Joiner COLON_JOINER = Joiner.on(COLON).useForNull("-");
    public final static Joiner POUND_SIGN_JOINER = Joiner.on(POUND_SIGN).useForNull("-");
    /**
     * 匹配图片url
     */
    private static final String IMGURL_REG = "(.*/)(.*)(\\..*)";


    public static String defaultString(String s) {
        return StringUtils.isEmpty(s) || "null".equals(s) ? EMPTY : s;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static String trimToEmpty(String str) {
        return str == null ? EMPTY : str.trim();
    }

    public static boolean equals(CharSequence cs1, CharSequence cs2) {
        return cs1 == null ? cs2 == null : cs1.equals(cs2);
    }

    public static int conventInt(String value, int defaultValue) {
        try {
            return Integer.parseInt(value);
        } catch (Throwable e) {
            return defaultValue;
        }
    }

    public static boolean conventBool(String value, boolean defaultValue) {
        try {
            return Boolean.parseBoolean(value);
        } catch (Throwable e) {
            return defaultValue;
        }
    }

    public static long conventLong(String value, long defaultValue) {
        try {
            return Long.parseLong(value);
        } catch (Throwable e) {
            return defaultValue;
        }
    }

    public static BigDecimal conventBigDecimal(String value, BigDecimal defaultValue) {
        try {
            return new BigDecimal(value);
        } catch (Throwable e) {
            return defaultValue;
        }
    }

    public static String[] split(String src, String separator) {
        return StringUtils.split(src, separator);
    }


    public static List<String> splitList(String str, String separatorChars) {
        String[] array = split(str, separatorChars);
        if (ArrayUtil.isEmpty(array)) {
            return Collections.emptyList();
        }
        return Arrays.asList(array);
    }

    /**
     * 将字符串数组转换成字符串
     * @param list
     * @param separator
     * @return
     */
    public static String mergeStringList2String(List<String> list, String separator){
        if(CollectionUtil.isEmpty(list)){
            return "";
        }


        StringBuilder sb = new StringBuilder("");
        for (String s : list) {
            sb.append(s).append(separator);
        }
        String result = sb.toString();
        return result.substring(0, result.length()-1);
    }

    public static String mergeStringList2String(List<String> list){
        String separator = ",";
        return mergeStringList2String(list, separator);
    }


    public static List<Long> splitLongList(String src, String separator, long defaultValue) {
        String[] array = split(src, separator);
        if (ArrayUtil.isEmpty(array)) {
            return Collections.emptyList();
        }
        List<Long> list = Lists.newArrayListWithExpectedSize(array.length);
        for (int i = 0; i < array.length; i++) {
            list.add(conventLong(array[i], defaultValue));
        }
        return list;
    }

    public static long[] splitLong(String src, String separator, long defaultValue) {
        String[] array = split(src, separator);
        if (ArrayUtil.isEmpty(array)) {
            return EMPTY_LONG_ARRAY;
        }
        long[] longArray = new long[array.length];
        for (int i = 0; i < array.length; i++) {
            longArray[i] = conventLong(array[i], defaultValue);
        }
        return longArray;
    }

    public static int[] splitInt(String src, String separator, int defaultValue) {
        String[] array = split(src, separator);
        if (ArrayUtil.isEmpty(array)) {
            return EMPTY_INT_ARRAY;
        }
        int[] intArray = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            intArray[i] = conventInt(array[i], defaultValue);
        }
        return intArray;
    }

    public static Set<Integer> splitIntSet(String src, String separator, int defaultValue) {
        String[] array = split(src, separator);
        if (ArrayUtil.isEmpty(array)) {
            return Collections.emptySet();
        }
        Set<Integer> set = Sets.newHashSetWithExpectedSize(array.length);
        for (int i = 0; i < array.length; i++) {
            set.add(conventInt(array[i], defaultValue));
        }
        return set;
    }

    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }

    /**
     * 获取带尺寸的图片路径
     *
     * @param imgUrl  https://pic.tujia.com/upload/qualifiedpics/day_170528/201705280817086786.jpg
     * @param imgSize
     * @return https://pic.tujia.com/upload/qualifiedpics/day_170528/thumb/201705280817086786_1500_1003.jpg
     */
    public static String buildImgUrlWithSize(String imgUrl, String imgSize) {
        return imgUrl.replaceAll(IMGURL_REG, "$1" + "thumb/" + "$2_" + imgSize + "$3");
    }

    public final static boolean isNumeric(String s) {
        if (s != null && !"".equals(s.trim())) {
            return s.matches("^[0-9]*$");
        } else {
            return false;
        }
    }
}
